<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * config Filename
 *
 * @access	public
 * @param	string
 * @return	string
 */
if ( ! function_exists('config'))
{
	function config($value)
	{
		$CI =& get_instance();
        return $CI->config->item($value);
	}
}

if ( ! function_exists('month_name'))
{
	function month_name($value)
	{
		switch($value):
            case '01'   :   $month = 'Januari';break;
            case '02'   :   $month = 'Februari';break; 
            case '03'   :   $month = 'Maret';break; 
            case '04'   :   $month = 'April';break; 
            case '05'   :   $month = 'Mei';break; 
            case '06'   :   $month = 'juni';break; 
            case '07'   :   $month = 'Juli';break;
            case '08'   :   $month = 'Agustus';break;   
            case '09'   :   $month = 'September';break; 
            case '10'   :   $month = 'Oktober';break; 
            case '11'   :   $month = 'November';break; 
            case '12'   :   $month = 'Desember';break;
            default     :   $month = 'Uknown';break; 
        endswitch;
        return $month;
	}
}

if ( ! function_exists('decimal'))
{
	function decimal($value,$range)
	{
	    if ($value!="")
            $data =  number_format($value,$range,'.',','); 
        else
            $data = "";      
		return $data; 
	}
    
}

if ( ! function_exists('indonesian_date'))
{
    function indonesian_date($date,$format='')
    {  
         $day   = substr($date,8,2);  
         $month = substr($date,5,2);	 
         $year  = substr($date,0,4);
    	 if($format)
    		$data	= $day.$format.$month.$format.$year;  
    	 else 
    		$data	= $day.'-'.$month.'-'.$year;  
         return	$data;        
    }	
}

if ( ! function_exists('indonesian_longDate'))
{
    function indonesian_longDate($date)
    {  
         $day   = substr($date,8,2);  
         $month = month_name(substr($date,5,2));	 
         $year  = substr($date,0,4);
  		 return $data	= $day.' '.$month.' '.$year;       
    }	
}

if ( ! function_exists('IndonesianNumericToWord'))
{
    function IndonesianNumericToWord($x)
    {
        $abil = array("", "Satu", "Dua", "Tiga", "Empat", "Lima", "Enam", "Tujuh", "Delapan", "Sembilan", "Sepuluh", "Sebelas");
        if ($x < 12)
            return " " . $abil[$x];
        elseif ($x < 20)
            return IndonesianNumericToWord($x - 10) . " Belas";
        elseif ($x < 100)
            return IndonesianNumericToWord($x / 10) . " Puluh" . IndonesianNumericToWord($x % 10);
        elseif ($x < 200)
            return " seratus" . IndonesianNumericToWord($x - 100);
        elseif ($x < 1000)
            return IndonesianNumericToWord($x / 100) . " Ratus" . IndonesianNumericToWord($x % 100);
        elseif ($x < 2000)
            return " seribu" . IndonesianNumericToWord($x - 1000);
        elseif ($x < 1000000)
            return IndonesianNumericToWord($x / 1000) . " Ribu" . IndonesianNumericToWord($x % 1000);
        elseif ($x < 1000000000)
            return IndonesianNumericToWord($x / 1000000) . " Juta" . IndonesianNumericToWord($x % 1000000);
        elseif ($x < 1000000000000)
            return IndonesianNumericToWord($x / 1000000000) . " Miliar" . IndonesianNumericToWord($x % 1000000000);
        elseif ($x < 1000000000000000)
            return IndonesianNumericToWord($x / 1000000000000) . " Trilyun" . IndonesianNumericToWord($x % 1000000000000);
    }
 }
 
if ( ! function_exists('EnglishNumericToWord'))
{
    function EnglishNumericToWord($number) {
    	$arraye = explode(".",$number);
    	$dollar = NumberSplit($arraye[0]);
        if(!empty($arraye[1]))
            $arraye[1] = $arraye[1];
        else
            $arraye[1] = "";
        
    	$cent = NumberSplit($arraye[1]);
    	
    	$cekcent = $arraye[1];
    		if ($cekcent==0) {
    			$number = $dollar." DOLLARS ";
    		} else {
    			$cent = " AND ".$cent." CENTS";
    			$number = $dollar." DOLLARS ".$cent;
    		}
        return $number;
    }
    
    /** for 1 - 19 **/
    function OneDigit($number){
    	$one_digit=array('0'=>'zero','1'=>'one','2'=>'two','3'=>"three",'4'=>"four",'5'=>"five",'6'=>'six','7'=>'seven',
    	'8'=>'eight','9'=>'nine','10'=>'ten','11'=>'eleven',"12"=>'twelve','13'=>'thirteen','14'=>'fourteen',
    	'15'=>'fifteen','16'=>'sixteen','17'=>'seventeen','18'=>'eighteen','19'=>'nineteen','-'=>'minus');
        return $one_digit[$number];	
    }
    
    function TenDigit($number){
    	$ten_digit=array('2'=>'twenty','3'=>'thirty','4'=>'forty','5'=>'fifty','6'=>'sixty',
    	'7'=>'seventy','8'=>'eighty','9'=>'ninty');
        //$data = $ten_digit[$number];
        if(!empty($ten_digit[$number]))
            $data =  $ten_digit[$number];
        else
            $data = "";    
        return $data;
    }
    
    function NumberLong($number){
    	$number = strlen($number);
    	$numberlong=array('3'=>'hundred','4'=>'thousand','5'=>'thousand','6'=>'thousand','7'=>'million',
    	'8'=>'million','9'=>'million','10'=>'billion','11'=>'billion','12'=>'billion','13'=>'trillion','14'=>'trillion','15'=>'trillion');
        return $numberlong[$number];	
    }
    
    function EraseChar($number) {
	   $remove = array("'", "+", "*", "(", ")", " ", "%", ",");
	   $number = str_replace($remove, "", $number);
       return $number;
    }
    
    function is_number($element) {
       return preg_match ("/[^0-9]/", $element);
    }

    function str_rsplit($str,$sz){
        if ( !$sz ) { return false; }
        if ( $sz > 0 ) { return str_split($str,$sz); }   
        $l = strlen($str);
        $sz = min(-$sz,$l);
        $mod = $l % $sz;
        if ( !$mod ) { return str_split($str,$sz); }    
    
        return array_merge(array(substr($str,0,$mod)), str_split(substr($str,$mod),$sz));
    }
    
    function TwoDigit($number) {
       $number = intval($number);
   	   $count1 = substr($number, -2, 1);
   	   $count2 = substr($number, -1, 1);
   	   $counter1 = TenDigit($count1);
   	   $counter2 = OneDigit($count2);
       if(($number>=0) && ($number<=19)) {
			$number = OneDigit($number);
		} else {
			if ($count2 == 0) {
				$number = $counter1;
			} else {
				$number = $counter1."-".$counter2;
			}
		}
	   return $number;
    }
    
    function ThreeDigit($number) {
    	$count1 = substr($number, -3, 1);
    	$count2 = substr($number, -2, 2);
    	$counter1 = OneDigit($count1);
    	$counter2 = TwoDigit($count2);
    		if ($count2 == 0) {
    			$number = $counter1." hundred";
    		} else if ($count1 == 0) {
    			$number = TwoDigit($number);;
    		} else {
    			$number = $counter1." hundred ".$counter2;
    		}
    	return $number;
    } 

    function ThousandDigit($number) {
    	$NumberLong = NumberLong($number);
    	$splitnumb = str_rsplit($number,-3);
    		$ribuan = $splitnumb[0];
    		$ratusan = $splitnumb[1];
    		$nratusan = ThreeDigit($ratusan);
    		$nribuan = NumberSplit($ribuan);
    		if ($ratusan!=0) {
    			$and = " and ";
    		}
    		if ($ribuan==0) {
    			$number = $nratusan;
    		} else {
    			$number = $nribuan." ".$NumberLong.$and.$nratusan;
    		}
        return $number;
    }

    function MillionDigit($number) {
    	$NumberLong = NumberLong($number);
    	$splitnumb = str_rsplit($number,-3);
    	$jutaan = $splitnumb[0];
    	$ribuan = $splitnumb[1];
    	$ratusan = $splitnumb[2];
    	
    	$ribuan = $splitnumb[1].$splitnumb[2];
    	$nribuan = ThousandDigit($ribuan);
    	$njutaan = NumberSplit($jutaan);
    	if ($ribuan!=0) {
    			$and = " and ";
    		}
    	if ($jutaan==0) {
    		$number = $nribuan;
    	} else {
    		$number = $njutaan." ".$NumberLong.$and.$nribuan;
    	}
        return $number;
    }

    function BillionDigit($number) {
    	$NumberLong = NumberLong($number);
    	$splitnumb = str_rsplit($number,-3);
    	$milyaran = $splitnumb[0];
    	$jutaan = $splitnumb[1];
    	$ribuan = $splitnumb[2];
    	$ratusan = $splitnumb[3];
    	
    	$jutaan = $splitnumb[1].$splitnumb[2].$splitnumb[3];
    	$njutaan = MillionDigit($jutaan);
    	$nmilyaran = NumberSplit($milyaran);
    	if ($jutaan!=0) {
    			$and = " and ";
    		}
    	if($milyaran==0) {
    		$number = $njutaan;
    	} else {
    		$number = $nmilyaran." ".$NumberLong.$and.$njutaan;
    	}
        return $number;
    }

    function TrillionDigit($number) {
    	$NumberLong = NumberLong($number);
    	$splitnumb = str_rsplit($number,-3);
    	$triliunan = $splitnumb[0];
    	$milyaran = $splitnumb[1];
    	$jutaan = $splitnumb[2];
    	$ribuan = $splitnumb[3];
    	$ratusan = $splitnumb[4];
    	
    	$milyaran = $splitnumb[1].$splitnumb[2].$splitnumb[3].$splitnumb[4];
    	$nmilyaran = BillionDigit($milyaran);
    	$ntriliunan = NumberSplit($triliunan);
    	if ($milyaran!=0) {
    			$and = " and ";
    		}
    	if($triliunan==0) {
    		$number = $nmilyaran;
    	} else {
    		$number = $ntriliunan." ".$NumberLong.$and.$nmilyaran;
    	}
        return $number;
    }

    function NumberSplit($number){
    	$number = EraseChar($number);
    	if (($number >= 0) && ($number < 100)) {
    		$number = TwoDigit($number);
    	} else if (($number >= 100) && ($number < 1000)) {
    		$number = ThreeDigit($number);
    	} else if (($number >= 1000) && ($number < 1000000)) {
    		$number = ThousandDigit($number);
    	} else if (($number >= 1000000) && ($number < 1000000000)) {
    		$number = MillionDigit($number);
    	} else if (($number >= 1000000000) && ($number < 1000000000000)) {
    		$number = BillionDigit($number);
    	} else if (($number >= 1000000000000) && ($number < 1000000000000000)) {
    		$number = TrillionDigit($number);
    	} else {
    		$number = "Not Valid";
    	}
        return strtoupper($number);
    }
    
    
}



    function atur_baris2($uraian,$max_kar)
    {	
    	$uraian = addslashes($uraian);
    	//$penerima=addslashes($penerima);
    	$jml_uraian=strlen($uraian);
    	//$penerima=strlen($penerima);
    	
        if($jml_uraian > $penerima)
		   $kalimat=$uraian;	
	    else
		   $kalimat=$penerima;	
	   	 
     	$tampilkan="";
    	$arr_kalimat="";
    	$tampil="";
    	$j_tam=strlen($arr_kalimat);
	
        for($x=0;$x<$j_tam;$x++){
		   $arr_kalimat[$x]="";
	    }
	
    	$jml_kalimat=strlen($kalimat);
    	$i_kalimat=0; $jml_spasi=0;
    	$arr_kalimat=$kalimat;
    	while($i_kalimat < $jml_kalimat){
    		if($arr_kalimat[$i_kalimat]==' ')
    			$jml_spasi++;
    		$i_kalimat++;	
    	}
    	$jml_kata=$jml_spasi+1;
    	$tmp_pecah="";
    	$pecah_kalimat=explode(" ",$kalimat);
    	$j=0; $pindah_baris=0; $i_tampil=0; $sisa_karakter=$max_kar;
    	$i_banyak=0; $count=0; $jml_banyak=0; $banyak_pecah=0;  
    	while($j <= $jml_kata){
    		$panjang_kata=0;
    		if($pindah_baris==1) $sisa_karakter=$max_kar - $panjang_kata;
    		$panjang_kata=strlen($pecah_kalimat[$j]);
    		if($panjang_kata <= $sisa_karakter){
    			$tampilkan[$i_tampil]=$tampilkan[$i_tampil]." ".$pecah_kalimat[$j];
    			$sisa_karakter=$sisa_karakter - $panjang_kata - 1;
    			$panjang_kata2=strlen($pecah_kalimat[$j+1]);
    			if($panjang_kata2 <= $sisa_karakter) $pindah_baris=0;
    			else{
    				$pindah_baris=1;
    				$i_tampil++;
     			} 
     		}//end if($panjang_kata <= $sisa_karakter){
    		else{ //jika satu kata lebih besar dari space yang tersedia
    			$tmp_pecah="";
    			$tmp_pecah=$pecah_kalimat[$j];
    			$jml_banyak=strlen($tmp_pecah);
    			$banyak_pecah=ceil($jml_banyak / $max_kar);
    			$i_banyak=0; $count=0; $panjang_kata2=0;
    			while($i_banyak < $banyak_pecah){
    				if($pindah_baris==1) $sisa_karakter=$max_kar - $panjang_kata2;
    			
    				$batas=$max_kar * ($i_banyak+1);
    				for($y=$count; $y < $batas; $y++){
    					$tampilkan[$i_tampil]=$tampilkan[$i_tampil].$tmp_pecah[$y];
    					
    				}
    				$count=$y;
     				$panjang_kata2=$jml_banyak-$count;
    				$sisa_karakter=$sisa_karakter - $count - 1;
    				if($panjang_kata2 <= $sisa_karakter) $pindah_baris=0;
    				else{
    					$pindah_baris=1;
    					$i_tampil++;				
     				} 
    				$i_banyak++;	
    							
    			}
    		} 
    		$j++;
    	}
    	
    	$data=$i_tampil;
    	return $data;
    }       